package com.dzu.medium;


/**
 * @author ZhaoDong
 * @date 2023/3/3 17:41
 * @description 707. 设计链表
 */
public class Test707 {

    public static void main(String[] args) {
        MyLinkedList obj = new MyLinkedList();

        obj.addAtHead(7);
        obj.addAtHead(2);
        obj.addAtHead(1);
        System.out.println(obj);
        obj.addAtIndex(3, 0);
        System.out.println(obj);

        obj.deleteAtIndex(2);
        System.out.println(obj);

        obj.addAtHead(6);
        obj.addAtTail(4);
        System.out.println(obj.get(4));
        obj.addAtHead(4);

        obj.addAtIndex(5,0);
        obj.addAtHead(6);


        System.out.println(obj);



    }


    static class MyLinkedList {

        int size;
        Node head;

        public MyLinkedList() {
            size = 0;
            head = new Node(0);
        }

        public int get(int index) {
            if (index < 0 || index >= size) {
                return -1;
            }
            Node temp = head;

            while (index-- >= 0) {
                temp = temp.next;
            }
            return temp.val;
        }

        public void addAtHead(int val) {
            Node newNode = new Node(val);
            Node temp = head;
            newNode.next = temp.next;
            temp.next = newNode;
            size++;
        }

        public void addAtTail(int val) {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            Node newNode = new Node(val);
            temp.next = newNode;
            size++;
        }

        public void addAtIndex(int index, int val) {
            if (index < 0) {
                addAtHead(val);
                return;
            }
            if (index == size) {
                addAtTail(val);
                return;
            }
            if (index > size) {
                return;
            }
            Node temp = head;
            while (--index >= 0) {
                temp = temp.next;
            }

            Node newNode = new Node(val);
            newNode.next = temp.next;
            temp.next = newNode;
            size++;
        }

        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            Node temp = head;
            while (--index >= 0) {
                temp = temp.next;
            }
            temp.next = temp.next.next;
            size--;
        }

        @Override
        public String toString() {
            return "MyLinkedList{" +
                    "head=" + head +
                    '}';
        }
    }
    static class Node{
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }
}
